package ejemplo;

import java.io.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;

public class ClienteREST {
	public static void main(String[] args) throws Exception {
		String request = "http://api.search.yahoo.com/WebSearchService/V1/webSearch?" + // WebSearchService
		"appid=CTVIC3V34EhHC0vUcJGwDAPlk9msAVIRnUXIY4czn02zVKBm2938ixnn14.1qmH10TADw--"+ // Api ID
		"&query=supermercado" + // Query
		"&results=3"; // # resultados
		
		// crear cliente HTTP
		HttpClient client = new HttpClient();
		GetMethod method = new GetMethod(request);
		// esto es necesario desde pc's UPF
		HostConfiguration conf = new HostConfiguration();
		conf.setProxy("proxy.upf.es", 8080);
		// enviar request
		int statusCode = client.executeMethod(conf, method);
		if (statusCode != HttpStatus.SC_OK) {
			System.err.println("Method failed: " + method.getStatusLine());
		}
		InputStream rstream = null;
		// obtener respuesta
		rstream = method.getResponseBodyAsStream();
		// imprimir la respuesta (XML)
		BufferedReader br = new BufferedReader(new InputStreamReader(rstream));
		String line;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
		br.close();
	}
}